package cn.wolfcode.web.controller;


import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission(name = "员工查询", expression = "employee:list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){
//            int a = 1 / 0 ;// 定义异常测试
            PageInfo<Employee> pageInfo = employeeService.query(qo);
            model.addAttribute("pageInfo", pageInfo);
            // 查询部门，用于分页过滤查询
            List<Department> departments = departmentService.selectAll();
            model.addAttribute("departments", departments);
            return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    public String input(Long id, Model model){
        // 查询部门，用于显示
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);

        // 用于回显所有的角色
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);

        if (id != null){ // 新增
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee",employee);
            // 用于回显该员工拥有的角色
            List<Role> ownRoles = roleService.selectRolesById(id);
            model.addAttribute("ownRoles", ownRoles);

            // ====去重左边 role====
            roles.removeAll(ownRoles);
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] roleIds){
        if (employee.getId() != null){  // 编辑
            employeeService.update(employee, roleIds);
        }else{ // 新增
            employeeService.insert(employee, roleIds);
        }
        return "redirect:/employee/list";
    }

}
